<?php
namespace Store\Model;
use Think\Model;

class StoreAddressModel extends Model
{
    protected $_validate = array(
        array( 'store_name', 'require', '联系人必须填写' ),
        array( 'store_mobile', 'require', '电话号码必须填写' ),
        array( 'address', 'require', '街道地址必须填写' ),
    );
    
    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_BOTH),
        array('status', '1', self::MODEL_INSERT),
    );
        
    /**
     * @地址列表
     *
     * @param int $storeID            
     * @return array
     */
    public function getAddressList($storeID)
    {
        if (empty($storeID)) {
            return false;
        }
        $where['store_id'] = $storeID;
        $where['status'] = 1;
        $result = $this->where($where)->order('is_default DESC, update_time DESC')->select();
        return $result;
    }

    
    /**
     * @获取一个地址
     *
     * @param int $memberID
     * @return array
     */
    public function getAddress($storeID, $type = 1)
    {
        if (empty($storeID)) {
            return false;
        }
        if ($type == 1){
            $where['store_id'] = $storeID;
        } else {
            $where['address_id'] = $storeID;
        }

        $where['status'] = 1;
        $result = $this->where($where)->order('is_default DESC, update_time DESC')->find();
        return $result;
    }
    

    /**
     * @添加&&更新地址信息
     *
     * @param int $memberID            
     * @param int $addressID            
     * @param int $data            
     * @return bool
     */
    public function updateAddress($storeID, $addressID, $data = array())
    {
        if (empty($storeID)) {
            return false;
        }
        $data = $this->create($data);
        if(empty($data)){
            return false;
        }
        if (empty($addressID)) {
            $data['store_id'] = $storeID;
            $result = $this->add($data);
            if ($data['is_default'] && $result){
                $this->updateDefaultAddress($storeID, $result);
            }
        } else {
            $data['store_id'] = $storeID;
            $result = $this->where(array( 'store_id' => $storeID, 'address_id' => $addressID ))->save($data);
            if ($data['is_default'] && $result){
                $this->updateDefaultAddress($storeID, $addressID);
            }
        }
        return $result;
    }

    /**
     * @修改默认地址
     *
     * @param int $memberID
     * @param int $addressID
     * @param int $data
     * @return bool
     */
    public function updateDefaultAddress($storeID, $addressID)
    {
        if (empty($storeID) || empty($addressID)) {
            return false;
        }
        $this->startTrans();
        $rsA = $this->where(array( 'store_id' => $storeID ))->save(array( 'is_default' => 0, 'update_time' => time() ));
        $rsB = $this->where(array( 'store_id' => $storeID, 'address_id' => $addressID ))->save(array( 'is_default' => 1, 'update_time' => time() ));
        if (! $rsA || ! $rsB) {
            $this->rollback();
        }
        $result = $this->commit();
        return $result;
    }
    
}
?>